﻿using Bridge.Shared.Models;
using Bridge.Wms.Domain;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Orders;
using Orders.Input;
using Orders.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;

namespace Bridge.Wms.Application
{
    public class OrderService : ApplicationService, IOrderService
    {
        private readonly IRepository<Order, Guid> _orderRepository;
        public OrderService(IRepository<Order, Guid> orderRepository) {
        _orderRepository = orderRepository;
        }
        public async Task<PageResponseResult<OrderOutput>> GetOrderList(OrderInput input)
        {
            var query = (await _orderRepository.GetQueryableAsync()).WhereIf(!string.IsNullOrEmpty(input.VoucherCode), a => a.VoucherCode.Contains(input.VoucherCode));

            var totalCount = query.Count(); 
            var list = await query.Skip(input.PageSize * input.PageIndex).Take(input.PageSize).ToListAsync();
            var dto = list.Adapt<List<OrderOutput>>();
       
            return PageResponseResult<OrderOutput>.Success("ok", dto, input.PageSize, input.PageIndex, totalCount);
        }

        public async Task<ResponseResult<OrderOutput>> GetOrderInfo(Guid id)
        {
            var order = await _orderRepository.FirstOrDefaultAsync(a => a.Id == id);
            if (order == null)
            {
                return ResponseResult<OrderOutput>.Fail("记录不存在！");
            }

            var dto = order.Adapt<OrderOutput>();
            return ResponseResult<OrderOutput>.Success("ok", dto);
        }

        public async Task<ResponseResult> CreateOrUpdateOrder(CreateOrUpdateOrderInput input)
        {
            return null;
        }
    }
}
